<?php

namespace app\rental\controller;

use app\admin\model\RouteModel;
use cmf\controller\AdminBaseController;
use app\rental\model\RentalBrandModel;
use think\Db;
use app\admin\model\ThemeModel;

class AdminBrandController extends AdminBaseController
{
    /**
     * 品牌管理
     * @adminMenu(
     *     'name'   => '汽车品牌',
     *     'parent' => 'rental/AdminIndex/default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 3,
     *     'icon'   => '',
     *     'remark' => '汽车品牌',
     *     'param'  => ''
     * )
     */
    public function index()
    {
        $rentalBrandModel = new RentalBrandModel();
        $brands           = $rentalBrandModel->order("list_order desc")->paginate();

        $this->assign("arrStatus", $rentalBrandModel::$STATUS);
        $this->assign("brands", $brands);
        $this->assign('page', $brands->render());
        return $this->fetch();
    }

    /**
     * 添加汽车品牌
     * @adminMenu(
     *     'name'   => '添加汽车品牌',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 0,
     *     'icon'   => '',
     *     'remark' => '添加汽车品牌',
     *     'param'  => ''
     * )
     */
    public function add()
    {
        $rentalBrandModel = new RentalBrandModel();
        $this->assign("arrStatus", $rentalBrandModel::$STATUS);
        return $this->fetch();
    }

    /**
     * 添加汽车品牌提交
     * @adminMenu(
     *     'name'   => '添加汽车品牌提交',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 0,
     *     'icon'   => '',
     *     'remark' => '添加汽车品牌提交',
     *     'param'  => ''
     * )
     */
    public function addPost()
    {

        $arrData = $this->request->param();
        $result = $this->validate($arrData, 'RentalBrand');
        if ($result !== true) {
            $this->error($result);
        }
        $rentalBrandModel = new RentalBrandModel();
        $rentalBrandModel->isUpdate(false)->allowField(true)->save($arrData);

        $this->success(lang("SAVE_SUCCESS"),url('AdminBrand/index'));

    }
      /**
     * 编辑汽车品牌
     * @adminMenu(
     *     'name'   => '编辑汽车品牌',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 0,
     *     'icon'   => '',
     *     'remark' => '编辑汽车品牌',
     *     'param'  => ''
     * )
     */
    public function edit()
    {
        $id = $this->request->param('id', 0, 'intval');
        if ($id > 0) {
            $rentalBrandModel = new RentalBrandModel();
            $brand = $rentalBrandModel->get($id)->toArray();
            $this->assign("arrStatus", $rentalBrandModel::$STATUS);
            $this->assign("brand", $brand);
            return $this->fetch();
        } else {
            $this->error('操作错误!');
        }

    }
    public function editPost()
    {
        $arrData = $this->request->param();
        $intId     = $this->request->param("id");
        $result = $this->validate($arrData, 'RentalBrand');
        if ($result !== true) {
            $this->error($result);
        }
        if (empty($intId)) {
            $this->error('操作错误');
        }
        $rentalBrandModel = new RentalBrandModel();
        $rentalBrandModel->isUpdate(true)->allowField(true)->save($arrData, ["id" => $intId]);
        //$rentalBrandModel->isUpdate(true)->save(["status" => $intStatus,"name" => $intName,"list_order" => $intListOrder], ["id" => $intId]);
        $this->success(lang("SAVE_SUCCESS"), url('AdminBrand/index'));

    }

    /**
     * 更新汽车品牌状态
     * @adminMenu(
     *     'name'   => '更新标签状态',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 3,
     *     'icon'   => '',
     *     'remark' => '更新标签状态',
     *     'param'  => ''
     * )
     */
    public function upStatus()
    {
        $intId     = $this->request->param("id");
        $intStatus = $this->request->param("status");
        $intStatus = $intStatus ? 1 : 0;
        if (empty($intId)) {
            $this->error(lang("NO_ID"));
        }

        $rentalBrandModel = new RentalBrandModel();
        $rentalBrandModel->isUpdate(true)->save(["status" => $intStatus], ["id" => $intId]);

        $this->success(lang("SAVE_SUCCESS"));

    }

    /**
     * 删除汽车品牌
     * @adminMenu(
     *     'name'   => '删除汽车品牌',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 0,
     *     'icon'   => '',
     *     'remark' => '删除汽车品牌',
     *     'param'  => ''
     * )
     */
    public function delete()
    {
        $intId = $this->request->param("id", 0, 'intval');

        if (empty($intId)) {
            $this->error(lang("NO_ID"));
        }
        $rentalBrandModel = new RentalBrandModel();

        $rentalBrandModel->where(['id' => $intId])->delete();

        $this->success(lang("DELETE_SUCCESS"));
    }
}
